from flask import jsonify


class R:
    @staticmethod
    def success():
        mDict = {'code': 0, 'msg': "成功"}
        return jsonify(mDict)

    @staticmethod
    def fail(msg, code=9999):
        mDict = {'code': code, 'msg': msg}
        return jsonify(mDict)

    @staticmethod
    def data(data):
        mDict = {'code': 0, 'msg': "成功", "data": data}
        return jsonify(mDict)


def str2Hump(text):
    """
    下划线转小驼峰
    :param text:字符串
    :return:
    """
    arr = filter(None, text.lower().split('_'))
    res = ''
    j = 0
    for i in arr:
        if j == 0:
            res = i
        else:
            res = res + i[0].upper() + i[1:]
        j += 1
    return res


def objs2dicts(objs):
    """
    模型对象列表转字典对象列表
    :param objs: 模型对象列表
    :return:
    """
    mList = []
    for item in objs:
        mList.append(item.to_dict())
    return mList


def hump_to_underline(text):
    """
    驼峰转下划线
    :param text:
    :return:
    """
    res = []
    for index, char in enumerate(text):
        if char.isupper() and index != 0:
            res.append("_")
        res.append(char)
    return ''.join(res).lower()


def underline_to_hump(text):
    """
    下划线转大驼峰
    :param text:
    :return:
    """
    arr = text.lower().split('_')
    res = []
    for i in arr:
        res.append(i[0].upper() + i[1:])
    return ''.join(res)


def underline_to_camel(text):
    """
    下划线转小驼峰
    :param text:
    :return:
    """
    s = underline_to_hump(text)
    return s[0].lower() + s[1:]
